package com.link.play.fit.ai.pushWorker

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.link.play.fit.ai.GlobalConfig.NOTIFICATION_DRINKING_ID
import com.link.play.fit.ai.R
import com.link.play.fit.ai.kv.AuthCache.drinkingAlarmCount
import com.link.play.fit.ai.ui.SplashAc
import com.link.play.fit.ai.ui.main.MainAc
import com.link.play.fit.ai.utils.AppX.context

class DrinkPushWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
    // 存储字符串列表
    private var notifyList: MutableList<String> = mutableListOf()

    init {
        // 从资源文件中加载字符串数组
        val list: MutableList<String> = mutableListOf(
            context.getString(R.string.drinking_suggestion1_text),
            context.getString(R.string.drinking_suggestion2_text)
        )
        notifyList = list.toMutableList()
    }

    override fun doWork(): Result {
        sendNotification()
        return Result.success()
    }

    private fun sendNotification() {
        val channelId = "kcal_drink_channel_id"
        val channelName = "kcal_drink_channel_name"
        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            channel.setShowBadge(true)
            notificationManager.createNotificationChannel(channel)
        }
        if (drinkingAlarmCount > notifyList.size-1) {
            drinkingAlarmCount = 0
        }
        kotlin.runCatching {
            val intent = Intent(applicationContext, SplashAc::class.java)
            val pendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE)
            val remoteViews = RemoteViews(applicationContext.packageName, R.layout.custom_notification)
            val notification = NotificationCompat.Builder(applicationContext, channelId)
                .setContentTitle("Kcal AI")
                .setContentIntent(pendingIntent)
                .setContentText(notifyList[drinkingAlarmCount])
//                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setSmallIcon(R.mipmap.ic_notification)
                .setColor(applicationContext.getColor(R.color.color_17D995))
//                .setColor(ContextCompat.getColor(context, R.color.color_F0A670))
//                .setColor(Color.parseColor("#FF17D995"))

//                .setStyle(NotificationCompat.DecoratedCustomViewStyle())
//                .setCustomContentView(remoteViews)
//                .setLargeIcon(BitmapFactory.decodeResource(applicationContext.resources, R.mipmap.ic_notification))
                .build()
            notificationManager.notify(NOTIFICATION_DRINKING_ID, notification)
        }

        drinkingAlarmCount+=1
    }


}